<template>
    <div name="SellerView" class="SellerView-warp content-wrapp">
        <div class="ModelNumberView-header">
            <!--设置-->
            <el-dialog
                :title="'设置 ' + setModelNumber"
                :visible.sync="dialogVisible"
                width="60%"
                :before-close="handleClose"
            >
                <el-form :model="formInfo" label-width="148px">
                    <el-form-item
                        label="亚马逊安全库存天数"
                        :rules="[
                            {
                                required: true,
                                message: '亚马逊安全库存天数不能为空'
                            }
                        ]"
                    >
                        <input hidden :model="formInfo.productId" />

                        <el-input-number
                            :step="1"
                            :max="1000"
                            :min="0"
                            size="mini"
                            v-model="formInfo.amzSafetyDay"
                            controls-position="right"
                        ></el-input-number>天
                    </el-form-item>

                    <el-form-item
                        label="总安全库存天数"
                        :rules="[
                            {
                                required: true,
                                message: '总安全库存天数不能为空'
                            }
                        ]"
                    >
                        <el-input-number
                            :step="1"
                            :max="1000"
                            :min="0"
                            size="mini"
                            v-model="formInfo.totalSafetyDay"
                            controls-position="right"
                        ></el-input-number>天
                    </el-form-item>

                    <el-form-item
                        label="备货日均"
                        :rules="[
                            { required: true, message: '备货日均不能为空' }
                        ]"
                    >
                        = (历史日均比率 * 历史日均) + (预测日均比率 * 预测日均)
                        <span
                            style="color:red"
                        >注意: 历史日均比率 + 预测日均比率 = 1</span>
                        <br />
                        <el-input-number
                            :step="0.1"
                            :precision="2"
                            :max="1.0"
                            :min="0.0"
                            size="mini"
                            v-model="formInfo.lastUnitsAvgDayRatio"
                            controls-position="right"
                        ></el-input-number>历史日均比率
                        <el-input-number
                            :step="0.1"
                            :precision="2"
                            :max="1.0"
                            :min="0.0"
                            size="mini"
                            v-model="formInfo.estUnitsAvgDayRatio"
                            controls-position="right"
                        ></el-input-number>预测日均比率
                    </el-form-item>

                    <el-form-item label="亚马逊发货数量">
                        (亚马逊安全天数库存天数-亚马逊库存天数) *
                        备货日均
                    </el-form-item>

                    <el-form-item label="采购补货数量">(总安全库存天数-总库存天数) * 备货日均</el-form-item>

                    <el-form-item
                        label="提醒设置一"
                        :rules="[
                            { required: true, message: '提醒设置一不能为空' }
                        ]"
                    >
                        近
                        <el-input-number
                            :step="1"
                            :max="1000"
                            :min="0"
                            size="mini"
                            v-model="formInfo.bigLastUnitsAvgDay"
                            controls-position="right"
                        ></el-input-number>天日均 >=
                        <el-select size="mini" v-model="formInfo.bigItemKeyId">
                            <el-option
                                v-for="itemKey in estItemKeyList"
                                :key="itemKey.itemKeyId"
                                :value="itemKey.itemKeyId"
                                :label="itemKey.itemKey + itemKeyNameEst"
                            ></el-option>
                        </el-select>
                        <el-input-number
                            :step="0.1"
                            :precision="2"
                            :max="1.0"
                            :min="0.0"
                            size="mini"
                            placeholder="填写比率"
                            v-model="formInfo.bigEstUnitisAvgDayRatio"
                            controls-position="right"
                        ></el-input-number>预测数量
                    </el-form-item>

                    <el-form-item
                        label="提醒设置二"
                        :rules="[
                            { required: true, message: '提醒设置二不能为空' }
                        ]"
                    >
                        近
                        <el-input-number
                            :step="1"
                            :max="1000"
                            :min="0"
                            size="mini"
                            v-model="formInfo.samllLastUnitsAvgDay"
                            controls-position="right"
                        ></el-input-number>天日均 <=
                        <el-select size="mini" v-model="formInfo.samllItemKeyId">
                            <el-option
                                v-for="itemKey in estItemKeyList"
                                :key="itemKey.itemKeyId"
                                :value="itemKey.itemKeyId"
                                :label="itemKey.itemKey + itemKeyNameEst"
                            ></el-option>
                        </el-select>
                        <el-input-number
                            :step="0.1"
                            :precision="2"
                            :max="1.0"
                            :min="0.0"
                            size="mini"
                            placeholder="填写比率"
                            v-model="formInfo.samllEstUnitsAvgDayRatio"
                            controls-position="right"
                        ></el-input-number>预测数量
                    </el-form-item>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <!--<el-button type="primary"  size="mini" >修 改</el-button>-->
                    <el-button
                        type="primary"
                        size="mini"
                        @click="saveFromInfo"
                        v-if="
                            formInfo.productId != null &&
                                formInfo.productId != 0
                        "
                    >保 存</el-button>

                    <p
                        v-if="
                            formInfo.productId == null ||
                                formInfo.productId == 0
                        "
                    >
                        <span style="color:#ff4532;font-size:12px;">* 修改所有产品</span>
                        <el-button type="primary" size="mini" @click="saveFromInfo">保 存</el-button>
                    </p>
                </div>
            </el-dialog>

            <!--表单-->
            <el-form
                :model="searchInfo"
                method="post"
                enctype="multipart/form-data"
                label-width="80px"
            >
                <el-form-item label="BU" label-width="40px">
                    <el-select
                        placeholder="请选择BU"
                        filterable
                        multiple
                        collapse-tags
                        v-model="searchInfo.businessUnitId"
                        @change="chooseBU"
                        size="small"
                    >
                        <allAddDelSelect
                            :selectForm="searchInfo"
                            :selectArray="BUList"
                            selectFormId="businessUnitId"
                            selectArrayId="businessUnitId"
                            size="mini"
                            @selectAll="chooseBU"
                            @selectDel="chooseBU"
                        ></allAddDelSelect>
                        <el-option
                            v-for="bu in BUList"
                            :key="bu.businessUnitId"
                            :value="bu.businessUnitId"
                            :label="bu.businessUnit"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="产品系列">
                    <el-select
                        placeholder="请选择产品系列"
                        filterable
                        multiple
                        collapse-tags
                        v-model="searchInfo.productCategoryId"
                        @change="chooseProductCategory"
                        size="small"
                    >
                        <allAddDelSelect
                            :selectForm="searchInfo"
                            :selectArray="productCategoryList"
                            selectFormId="productCategoryId"
                            selectArrayId="productCategoryId"
                            size="mini"
                            @selectAll="chooseProductCategory"
                            @selectDel="chooseProductCategory"
                        ></allAddDelSelect>
                        <el-option
                            v-for="productCategory in productCategoryList"
                            :key="productCategory.productCategoryId"
                            :value="productCategory.productCategoryId"
                            :label="productCategory.productCategory"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="产品型号">
                    <el-select
                        placeholder="请选择产品型号"
                        filterable
                        multiple
                        collapse-tags
                        v-model="searchInfo.productId"
                        size="small"
                    >
                        <allAddDelSelect
                            :selectForm="searchInfo"
                            :selectArray="productList"
                            selectFormId="productId"
                            selectArrayId="productId"
                            size="mini"
                        ></allAddDelSelect>
                        <el-option
                            v-for="product in productList"
                            :key="product.productId"
                            :value="product.productId"
                            :label="product.productModelNumber"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="账号" label-width="50px">
                    <el-select
                        placeholder="请选择账号"
                        filterable
                        multiple
                        collapse-tags
                        v-model="searchInfo.brandId"
                        size="small"
                        @change="chooseBrandId"
                    >
                        <allAddDelSelect
                            :selectForm="searchInfo"
                            :selectArray="brandList"
                            selectFormId="brandId"
                            selectArrayId="brandId"
                            size="mini"
                        ></allAddDelSelect>
                        <el-option
                            v-for="brand in brandList"
                            :key="brand.brandId"
                            :value="brand.brandId"
                            :label="brand.brandName"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="国家" label-width="50px">
                    <el-select
                        placeholder="请选择国家"
                        filterable
                        multiple
                        collapse-tags
                        v-model="searchInfo.countryId"
                        @change="chooseCountry"
                        size="small"
                    >
                        <allAddDelSelect
                            :selectForm="searchInfo"
                            :selectArray="countryList"
                            selectFormId="countryId"
                            selectArrayId="countryId"
                            size="mini"
                        ></allAddDelSelect>
                        <el-option
                            v-for="country in countryList"
                            :key="country.countryId"
                            :value="country.countryId"
                            :label="country.countryName"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label-width="15px">
                    <el-checkbox v-model="afnFulfillableQuantityNull">隐藏亚马逊可售为0</el-checkbox>
                </el-form-item>
                <el-form-item label-width="15px">
                    <el-button
                        type="primary"
                        @click="searchForm"
                        size="small"
                        icon="el-icon-search"
                    >搜索</el-button>
                </el-form-item>

                <dataColumSetting
                    ref="dataColum"
                    :columns="dataColumn"
                    :allColumns="allDataColumn"
                    w="615px"
                    @updColumns="upDataColumn"
                    size="small"
                ></dataColumSetting>
                <download
                    ref="download"
                    :columns="dataColumnShow"
                    :allColumns="allDataColumn"
                    @getDataxlsx="getDataxlsx"
                    size="small"
                    title="账号汇总"
                ></download>
                <Upload></Upload>
                <pagination
                    :pagination="pagination"
                    :length="total"
                    @currpage="search"
                    float="right"
                ></pagination>
            </el-form>
        </div>
        <div class="table-content">
            <!--表格-->
            <el-table
                ref="table"
                name="showTable"
                border="border"
                v-loading="loading"
                :data="InventoryList"
            >
                <!-- :row-class-name="tableRowClassName"-->

                <el-table-column key="1" label="操作">
                    <template slot-scope="scope">
                        <el-button
                            type="warning"
                            @click="addOrUpdateISetting(scope.row)"
                            size="mini"
                        >设置</el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    key="2"
                    prop="brandName"
                    label="账号"
                    v-if="!dataColumnShow.brandName"
                ></el-table-column>

                <el-table-column
                    key="3"
                    prop="countryName"
                    label="国家"
                    v-if="!dataColumnShow.countryName"
                ></el-table-column>

                <el-table-column
                    key="4"
                    prop="productCategory"
                    label="系列"
                    v-if="!dataColumnShow.productCategory"
                ></el-table-column>

                <el-table-column
                    key="5"
                    prop="productModelNumber"
                    label="型号"
                    v-if="!dataColumnShow.productModelNumber"
                >
                    <template slot-scope="scope">
                        <el-link
                            :href="
                                '#/productCenter/productBasicInfo?model=' +
                                    scope.row.productModelNumber
                            "
                        >{{ scope.row.productModelNumber }}</el-link>
                    </template>
                </el-table-column>

                <el-table-column
                    key="6"
                    label="日均"
                    v-if="
                        !dataColumnShow.lastUnitsAvgDay ||
                            !dataColumnShow.stockingAvgDay ||
                            !dataColumnShow.estUnitsAvgDay
                    "
                >
                    <el-table-column
                        key="6-1"
                        prop="lastUnitsAvgDay"
                        label="加权历史日均"
                        v-if="!dataColumnShow.lastUnitsAvgDay"
                    >
                        <template slot-scope="scope">
                            <el-popover trigger="hover" placement="top">
                                <p>
                                    <span
                                        v-for="(itemKeyObj, index) in scope.row
                                            .lastItemKeyList"
                                        :key="itemKeyObj.itemKeyId"
                                    >
                                        {{ itemKeyObj.itemKey
                                        }}{{ itemKeyNameLast }}
                                        <span
                                            v-if="
                                                index <
                                                    scope.row.lastItemKeyList
                                                        .length -
                                                        1
                                            "
                                        >+</span>
                                    </span>
                                </p>

                                <div slot="reference" class="name-wrapper">
                                    <span>{{ scope.row.lastUnitsAvgDay }}</span>
                                </div>
                            </el-popover>
                        </template>
                    </el-table-column>
                    <el-table-column
                        key="6-2"
                        prop="estUnitsAvgDay"
                        label="加权预测日均"
                        v-if="!dataColumnShow.estUnitsAvgDay"
                    >
                        <template slot-scope="scope">
                            <el-popover trigger="hover" placement="top">
                                <p>
                                    <span
                                        v-for="(itemKeyObj, index) in scope.row
                                            .estItemKeyList"
                                        :key="itemKeyObj.itemKeyId"
                                    >
                                        {{ itemKeyObj.itemKey
                                        }}{{ itemKeyNameEst }}
                                        <span
                                            v-if="
                                                index <
                                                    scope.row.estItemKeyList
                                                        .length -
                                                        1
                                            "
                                        >+</span>
                                    </span>
                                </p>

                                <div slot="reference" class="name-wrapper">
                                    <span>{{ scope.row.estUnitsAvgDay }}</span>
                                </div>
                            </el-popover>
                        </template>
                    </el-table-column>
                    <el-table-column
                        key="6-3"
                        prop="stockingAvgDay"
                        label="备货日均"
                        v-if="!dataColumnShow.stockingAvgDay"
                    >
                        <template slot-scope="scope">
                            <el-popover trigger="hover" placement="top">
                                <p>加权历史日均 + 加权预测日均</p>

                                <div slot="reference" class="name-wrapper">
                                    <span>{{ scope.row.stockingAvgDay }}</span>
                                </div>
                            </el-popover>
                        </template>
                    </el-table-column>
                </el-table-column>

                <el-table-column
                    key="7"
                    prop="estUnitsPromotion"
                    label="活动预测量"
                    v-if="!dataColumnShow.estUnitsPromotion"
                ></el-table-column>

                <!--亚马逊模块-->

                <el-table-column
                    key="8"
                    label="亚马逊"
                    v-if="
                        !dataColumnShow.afnFulfillableQuantity ||
                            !dataColumnShow.shippedAndReceivingQuantity ||
                            !dataColumnShow.amzTotalInventoryQuantity ||
                            !dataColumnShow.amzFulfillableQuantityDay
                    "
                >
                    <el-table-column
                        key="8-1"
                        prop="afnFulfillableQuantity"
                        label="可售"
                        v-if="!dataColumnShow.afnFulfillableQuantity"
                    ></el-table-column>
                    <!-- <el-table-column prop="amzInventoryVO.afnUnsellableQuantity" label="不可卖"  ></el-table-column>-->
                    <el-table-column
                        key="8-2"
                        prop="amzInventoryVO.shippedAndReceivingQuantity"
                        label="在途"
                        v-if="!dataColumnShow.shippedAndReceivingQuantity"
                    ></el-table-column>
                    <el-table-column
                        key="8-3"
                        prop="amzInventoryVO.amzTotalInventoryQuantity"
                        label="亚马逊总库存"
                        v-if="!dataColumnShow.amzTotalInventoryQuantity"
                    ></el-table-column>
                    <el-table-column
                        key="8-4"
                        prop="amzInventoryVO.amzFulfillableQuantityDay"
                        label="总可售天数"
                        v-if="!dataColumnShow.amzFulfillableQuantityDay"
                    ></el-table-column>
                </el-table-column>

                <el-table-column
                    key="9"
                    prop="amzSafetyDay"
                    label="亚马逊安全库存天数"
                    v-if="!dataColumnShow.amzSafetyDay"
                ></el-table-column>

                <el-table-column
                    key="10"
                    label="亚马逊需补发数量"
                    v-if="
                        !dataColumnShow.amzReplenishmentDay ||
                            !dataColumnShow.replenishmentQuantity
                    "
                >
                    <el-table-column
                        key="10-1"
                        prop="amzInventoryVO.amzReplenishmentDay"
                        label="补货天数"
                        v-if="!dataColumnShow.amzReplenishmentDay"
                    ></el-table-column>
                    <el-table-column
                        key="10-2"
                        prop="amzInventoryVO.replenishmentQuantity"
                        label="补货数量"
                        v-if="!dataColumnShow.replenishmentQuantity"
                    ></el-table-column>
                </el-table-column>

                <!--深圳仓模块-->

                <el-table-column
                    key="11"
                    label="深圳仓"
                    v-if="
                        !dataColumnShow.fnInventoryQuantity ||
                            !dataColumnShow.upcInventoryQuantity ||
                            !dataColumnShow.localInventoryTotalQuantity
                    "
                >
                    <el-table-column
                        key="11-1"
                        prop="fnInventoryQuantity"
                        label="FN库存"
                        v-if="!dataColumnShow.fnInventoryQuantity"
                    ></el-table-column>
                    <el-table-column
                        key="11-2"
                        prop="upcInventoryQuantity"
                        label="UPC库存"
                        v-if="!dataColumnShow.upcInventoryQuantity"
                    ></el-table-column>
                    <el-table-column
                        key="11-3"
                        label="深圳仓总库存"
                        v-if="!dataColumnShow.localInventoryTotalQuantity"
                    >
                    <template slot-scope="scope">
                       {{scope.row.fnInventoryQuantity + scope.row.upcInventoryQuantity}}
                      </template>
                    </el-table-column>
                </el-table-column>

                <!--供应商模块-->
                <el-table-column
                    key="12"
                    label="供应商"
                    v-if="
                            (!dataColumnShow.supplierName ||
                                !dataColumnShow.iTaxUnitPrice ||
                                !dataColumnShow.unclearedQuantity ||
                                !dataColumnShow.supplierInventoryTotalQuantity)
                    "
                >
                    <el-table-column
                        key="12-1"
                        prop="supplierInventoryVO.supplierName"
                        label="供应商"
                        v-if="
                                !dataColumnShow.supplierName
                        "
                    ></el-table-column>
                    <!-- <el-table-column  label="最小包"  ></el-table-column>-->
                    <el-table-column
                        key="12-2"
                        prop="supplierInventoryVO.iTaxUnitPrice"
                        label="采购含税单价"
                        v-if="
                                !dataColumnShow.iTaxUnitPrice
                        "
                    ></el-table-column>
                    <!-- <el-table-column  label="可发数量"  ></el-table-column>-->
                    <el-table-column
                        key="12-3"
                        prop="supplierInventoryVO.unclearedQuantity"
                        label="未清订单"
                        v-if="
                                !dataColumnShow.unclearedQuantity
                        "
                    ></el-table-column>
                    <el-table-column
                        key="12-4"
                        prop="supplierInventoryVO.supplierInventoryTotalQuantity"
                        label="供应商总库存"
                        v-if="
                                !dataColumnShow.supplierInventoryTotalQuantity
                        "
                    ></el-table-column>
                </el-table-column>

                <!--总库存-->
                <el-table-column
                    key="13"
                    label="总库存"
                    v-if="
                        !dataColumnShow.totalInventory ||
                            !dataColumnShow.totalInventoryDay
                    "
                >
                    <el-table-column
                        key="13-1"
                        prop="totalInventory"
                        label="总库存"
                        v-if="!dataColumnShow.totalInventory"
                    ></el-table-column>
                    <el-table-column
                        key="13-2"
                        prop="totalInventoryDay"
                        label="总库存天数"
                        v-if="!dataColumnShow.totalInventoryDay"
                    ></el-table-column>
                </el-table-column>

                <el-table-column
                    key="14"
                    prop="totalSafetyDay"
                    label="总安全库存天数"
                    v-if="!dataColumnShow.totalSafetyDay"
                ></el-table-column>

                <!--补货需求-->
                <el-table-column
                    key="15"
                    label="补货需求"
                    v-if="
                        !dataColumnShow.replenishmentDay ||
                            !dataColumnShow.replenishmentQuantity
                    "
                >
                    <el-table-column
                        key="15-1"
                        prop="replenishmentDay"
                        label="补货天数"
                        v-if="!dataColumnShow.replenishmentDay"
                    ></el-table-column>
                    <el-table-column
                        key="15-2"
                        prop="replenishmentQuantity"
                        label="补货数量"
                        v-if="
                            !dataColumnShow.amzInventoryVOreplenishmentQuantity
                        "
                    ></el-table-column>
                </el-table-column>

                <el-table-column key="16" label="运营备注" v-if="!dataColumnShow.salesRemark">
                    <template slot-scope="scope">
                        <span
                            v-if="
                                scope.row.salesRemark == null ||
                                    salesRemark == ''
                            "
                        >
                            <RemarkDialog
                                ref="remarkDialog"
                                :id="scope.row.productId"
                                :title="salesRemark"
                                :search="search"
                            ></RemarkDialog>
                        </span>
                        <span
                            v-if="
                                scope.row.salesRemark != null &&
                                    salesRemark != ''
                            "
                        >
                            <el-popover trigger="hover" placement="top">
                                <p>{{ scope.row.salesRemark }}</p>
                                <div slot="reference" class="name-wrapper">
                                    <el-tag size="mini">查看备注</el-tag>
                                    <RemarkDialog
                                        ref="remarkDialog"
                                        :id="scope.row.productId"
                                        :title="salesRemark"
                                        :search="search"
                                    ></RemarkDialog>
                                </div>
                            </el-popover>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column key="17" label="采购备注" v-if="!dataColumnShow.purchaseRemark">
                    <template slot-scope="scope">
                        <span
                            v-if="
                                scope.row.purchaseRemark == null ||
                                    purchaseRemark == ''
                            "
                        >
                            <RemarkDialog
                                ref="remarkDialog"
                                :id="scope.row.productId"
                                :title="purchaseRemark"
                                :search="search"
                            ></RemarkDialog>
                        </span>
                        <span
                            v-if="
                                scope.row.purchaseRemark != null &&
                                    purchaseRemark != ''
                            "
                        >
                            <el-popover trigger="hover" placement="top">
                                <p>{{ scope.row.purchaseRemark }}</p>
                                <div slot="reference" class="name-wrapper">
                                    <el-tag size="mini">查看备注</el-tag>
                                    <RemarkDialog
                                        ref="remarkDialog"
                                        :id="scope.row.productId"
                                        :title="purchaseRemark"
                                        :search="search"
                                    ></RemarkDialog>
                                </div>
                            </el-popover>
                        </span>
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                    :pagination="pagination"
                    :length="total"
                    @currpage="search"
                    float="right"
                ></pagination>
            <scrollTable ref="scrollTable"></scrollTable>
        </div>
    </div>
</template>

<script>
const ERR_OK = 200;
import pagination from "components/pagination/pagination.vue";
import scrollTable from "components/scroll/scroll-table.vue";
export default {
    name: "sellerView",
    components: {
        RemarkDialog: resolve => require(["./RemarkDialog.vue"], resolve),
        pagination,
        scrollTable,
        allAddDelSelect: resolve =>
            require(["components/searchfor/allAddDelSelect.vue"], resolve),
        dataColumSetting: resolve =>
            require([
                "components/dataColumSetting/dataColumSetting.vue"
            ], resolve),
        download: resolve => require(["components/xlsx/xlsx.vue"], resolve),
        Upload: resolve => require(["./upload.vue"], resolve),
    },
    data() {
        return {
            afnFulfillableQuantityNull: false,
            pagination: {
                pagesize: 20,
                currpage: 1
            },
            total: 0,
            loading: true,
            dialogVisible: false,
            salesRemark: "运营备注",
            purchaseRemark: "采购备注",
            itemKeyNameLast: "天历史日均",
            itemKeyNameEst: "天预测日均",
            userId: this.$store.getters.userId,

            //搜索表单信息
            searchInfo: {
                businessUnitId: [],
                productCategoryId: [],
                productId: [],
                brandId: [],
                countryId: []
            },

            //搜索下拉列表
            BUList: [],
            productCategoryList: [],
            productList: [],
            countryList: [],
            brandList: [], //账号list

            //自定义列 预测列
            estItemKeyList: [],

            //表格 lsit
            InventoryList: [],
            setModelNumber: "", //对话框的标题名 显示对应修改的modelNumber

            //提交表单
            formInfo: {
                productId: 0,
                amzSafetyDay: 0,
                totalSafetyDay: 0,

                //加权历史日均占比 和加权预测日均占比
                lastUnitsAvgDayRatio: 0.0,
                estUnitsAvgDayRatio: 0.0,

                //大于方向
                bigLastUnitsAvgDay: 0,
                bigItemKeyId: 0,
                bigEstUnitisAvgDayRatio: 0.0,

                //小于方向
                samllLastUnitsAvgDay: 0,
                samllItemKeyId: 0,
                samllEstUnitsAvgDayRatio: 0.0,

                userId: 0
            },
            //数据列
            dataColumnShow: {},
            dataColumn: [],
            allDataColumn: [
                { property: "brandName", label: "账号" },
                { property: "countryName", label: "国家" },
                { property: "productCategory", label: "系列" },
                { property: "productModelNumber", label: "型号" },
                { property: "lastUnitsAvgDay", label: "加权历史日均" },
                { property: "estUnitsAvgDay", label: "加权预测日均" },
                { property: "stockingAvgDay", label: "备货日均" },
                { property: "estUnitsPromotion", label: "活动预测量" },
                { property: "afnFulfillableQuantity", label: "可售" },
                { property: "shippedAndReceivingQuantity", label: "在途" },
                {
                    property: "amzTotalInventoryQuantity",
                    label: "亚马逊总库存"
                },
                { property: "amzFulfillableQuantityDay", label: "总可售天数" },
                { property: "amzSafetyDay", label: "亚马逊安全库存天数" },
                { property: "amzReplenishmentDay", label: "补货天数" },
                {
                    property: "amzInventoryVOreplenishmentQuantity",
                    label: "补货数量"
                },
                { property: "fnInventoryQuantity", label: "FN库存" },
                { property: "upcInventoryQuantity", label: "UPC库存" },
                {
                    property: "localInventoryTotalQuantity",
                    label: "深圳仓总库存"
                },
                { property: "supplierName", label: "供应商" },
                { property: "iTaxUnitPrice", label: "采购含税单价" },
                { property: "unclearedQuantity", label: "未清订单" },
                {
                    property: "supplierInventoryTotalQuantity",
                    label: "供应商总库存"
                },
                { property: "totalInventory", label: "总库存" },
                { property: "totalInventoryDay", label: "总库存天数" },
                { property: "totalSafetyDay", label: "总安全库存天数" },
                { property: "replenishmentDay", label: "补货天数" },
                { property: "replenishmentQuantity", label: "补货数量" },
                { property: "salesRemark", label: "运营备注" },
                { property: "purchaseRemark", label: "采购备注" }
            ]
        };
    },
    created() {
        this.findColumn();
        this.init();
    },
    methods: {
        init() {
            this.initBU();
            this.initProductCategory();
            this.initModelNumber();
            this.initEstItemKeyList();
            this.initCountryList();
            this.initBrandList();
            this.search();
        },
        initBU() {
            //所有BU
            this.$ajax
                .get(this.GLOBAL.productService + "/businessUnit/findAllBU", {})
                .then(res => {
                    if (res.data.code == "200") {
                        var data = res.data.data;
                        this.BUList = data;
                    }
                });
        },
        initProductCategory() {
            //所有产品系列
            var url =
                this.GLOBAL.productService +
                "/productSubscription/findProductCategoryAll";
            this.$ajax.get(url, {}).then(res => {
                if (res.data.code == "200") {
                    var data = res.data.data;
                    this.productCategoryList = data;
                }
            });
        },
        initModelNumber() {
            //所有产品型号
            var url =
                this.GLOBAL.productService +
                "/productSubscription/findModelNumberAll";
            this.$ajax.get(url, {}).then(res => {
                if (res.data.code == "200") {
                    var data = res.data.data;
                    this.productList = data;
                }
            });
        },
        initEstItemKeyList() {
            var url =
                this.GLOBAL.inventory +
                "/itemKeyHandler/findEstItemKeyListByUserId/" +
                this.userId;
            this.$ajax.get(url, {}).then(res => {
                if (res.data.code == "200") {
                    var data = res.data.data;
                    this.estItemKeyList = data;
                }
            });
        },
        initCountryList() {
            var url =
                this.GLOBAL.productService + "/findProductCharge/findCountry";
            this.$ajax
                .get(url, {})
                .then(res => {
                    if (res.data.code == "200") {
                        this.countryList = res.data.data;
                    }
                })
                .catch(error => {
                    // console.logerror)
                });
        },
        initBrandList() {
            var url = this.GLOBAL.inventory + "/brandHandler/findBrandList";
            this.$ajax.get(url, {}).then(res => {
                if (res.data.code == "200") {
                    var data = res.data.data;
                    this.brandList = data;
                }
            });
        },
        chooseBU() {
            this.searchInfo.productCategoryId = [];
            this.productCategoryList = [];
            this.productList = [];
            this.searchInfo.productId = [];
            var buId = this.searchInfo.businessUnitId;
            //清空系列
            var url1 =
                this.GLOBAL.productService +
                "/productSubscription/findProductCategoryByBuId?buId=" +
                buId;

            this.$ajax
                .get(url1, {})
                .then(res => {
                    if (res.data.code == "200") {
                        this.productCategoryList = res.data.data;
                    }
                })
                .then(error => {
                    console.log(error);
                });
        },
        chooseProductCategory() {
            this.productList = [];
            this.searchInfo.productId = [];
            var productCategoryId = this.searchInfo.productCategoryId;
            var businessUnitId = this.searchInfo.businessUnitId;

            //清空 产品型号
            var url =
                this.GLOBAL.inventory +
                "/inventoryByModelNumberHandler/findModelNumberByproductCategoryIdAndBusinessUnitId?" +
                "productCategoryId=" +
                productCategoryId +
                "&businessUnitId=" +
                businessUnitId;
            console.log(url);
            //var url =  this.GLOBAL.productService + "productSubscription/findUserChargeByDeptId/" + deptId
            this.$ajax
                .get(url, {})
                .then(res => {
                    if (res.data.code == "200") {
                        console.log(res.data);
                        //清空负责人
                        this.productList = res.data.data;
                    }
                })
                .then(error => {
                    console.log(error);
                });
        },
        chooseBrandId() {
            var brandId = this.searchInfo.brandId;

            //清空 产品型号
            var url =
                this.GLOBAL.inventory +
                "/inventoryBySellerHandler/findModelNumberByBrandId?brandId=" +
                brandId;
            console.log(url);
            //var url =  this.GLOBAL.productService + "productSubscription/findUserChargeByDeptId/" + deptId
            this.$ajax
                .get(url, {})
                .then(res => {
                    if (res.data.code == "200") {
                        console.log(res.data);

                        this.productList = [];
                        this.productList = res.data.data;
                    }
                })
                .then(error => {
                    console.log(error);
                });
        },
        chooseCountry() {
            var brandId = this.searchInfo.brandId;
            var countryId = this.searchInfo.countryId;

            console.log("productCategoryId =" + productCategoryId);
            console.log("businessUnitId =" + businessUnitId);

            //清空 产品型号
            var url =
                this.GLOBAL.inventory +
                "/inventoryBySellerHandler/findModelNumberByBrandIdAndCountryId?brandId=" +
                brandId +
                "&countryId=" +
                countryId;
            console.log(url);
            //var url =  this.GLOBAL.productService + "productSubscription/findUserChargeByDeptId/" + deptId
            this.$ajax
                .get(url, {})
                .then(res => {
                    if (res.data.code == "200") {
                        console.log(res.data);

                        this.productList = [];
                        this.productList = res.data.data;
                    }
                })
                .then(error => {
                    console.log(error);
                });
        },
        searchForm() {
            this.pagination.currpage = 1;
            this.total = 0;
            this.search();
        },
        search() {
            this.InventoryList = []
            this.loading = true;
            var url =
                this.GLOBAL.inventory +
                "/inventoryBySellerHandler/findSellerInventoryList";

            var data = {
                businessUnitIdList: this.searchInfo.businessUnitId,
                productCategoryIdList: this.searchInfo.productCategoryId,
                productIdList: this.searchInfo.productId,
                countryIdList: this.searchInfo.countryId,
                brandIdList: this.searchInfo.brandId,
                pageSize: this.pagination.pagesize,
                pageNumber: this.pagination.currpage,
                afnFulfillableQuantityNull: this.afnFulfillableQuantityNull
            };

            this.$ajax({
                method: "post",
                url: url,
                data: data
            })
                .then(res => {
                    if (res.data.code == 200) {
                        this.InventoryList = res.data.data.data;
                        this.total = res.data.data.total;
                    } else if (res.data.code == 500) {
                        this.$message.error(res.data.msg);
                    }
                    this.loading = false;
                    this.$refs.scrollTable.scrollShow(this.$refs.table); 
                })
                .catch(err => {
                    console.log(err);
                });
        },
        tableRowClassName({ row, rowIndex }) {
            var red = row.redRemind;
            var blue = row.blueRemind;
            if (red) {
                //  console.log("red")
                return "red_warning-row";
            } else if (blue) {
                //   console.log("blue")
                return "blue_warning-row";
            }
            // if(row.amzInventoryVO.afnFulfillableQuantity === 0){
            //   return  this.afnFulfillableQuantity?'none':''
            // }
            return "";
        },
        addOrUpdateISetting(row) {
            console.log("addOrUpdateISetting");
            console.log(row.productId);

            this.formInfo.productId = row.productId;
            this.setModelNumber = row.productModelNumber;
            //查询预测相关自定义列
            this.getItemKeyList(row.productId);
            this.fillFormInfo(row.productId);

            //显示
            this.dialogVisible = true;
            // this.search()
        },
        saveFromInfo() {
            console.log("saveFromInfo");
            console.log(this.formInfo);

            if (this.checkFormInfo()) {
                return;
            }

            if (
                this.formInfo.productId == null ||
                this.formInfo.productId == "" ||
                this.formInfo.productId == 0
            ) {
                //全部保存
                this.$confirm("确定修改所有产品", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.saveALlProductInventory();
                });
            } else {
                //保存一行
                this.saveOneRowProductInventory();
            }

            this.dialogVisible = false;
            this.search();
        },
        handleClose() {
            //关闭对话框 清空表单
            this.setModelNumber = "";
            this.clearFormInfo();
            this.dialogVisible = false;
        },
        clearFormInfo() {
            //清除 表单设置的数据
            this.formInfo = {
                productId: 0,
                amzSafetyDay: 0,
                totalSafetyDay: 0,
                lastUnitsAvgDayRatio: 0,
                estUnitsAvgDayRatio: 0,
                bigLastUnitsAvgDay: 0,
                bigItemKeyId: 0,
                bigEstUnitisAvgDayRatio: 0.0,
                samllLastUnitsAvgDay: 0,
                samllItemKeyId: 0,
                samllEstUnitsAvgDayRatio: 0,
                userId: 0
            };
        },
        getItemKeyList(productId) {
            var url =
                this.GLOBAL.inventory +
                "/itemKeyHandler/findItemKeyByProductId/" +
                productId;
            this.$ajax.get(url, {}).then(res => {
                if (res.data.code == 200) {
                    this.estItemKeyList = res.data.data;
                    //    this.$message.success(res.data.msg)
                } else if (res.data.code == 500) {
                    this.$message.error(res.data.msg);
                }
            });
        },
        fillFormInfo(productId) {
            //当前产品存在数据 填充数据
            var url =
                this.GLOBAL.inventory +
                "/inventoryByModelNumberHandler/findProductInventoryInfo/" +
                productId;
            this.$ajax.get(url, {}).then(res => {
                if (res.data.code == 200) {
                    //this.estItemKeyList = res.data.data
                    this.formInfo = res.data.data;
                    //   this.$message.success(res.data.msg)
                } else if (res.data.code == 500) {
                    this.$message.error(res.data.msg);
                }
            });
        },
        checkFormInfo() {
            var flag = false;
            var product = this.formInfo.productId;
            if (product == null || product == 0) {
                flag = true;
                this.$message.error("产品id为空请先关闭窗口重新打开");
                return flag;
            }

            var totalSafetyDay = this.formInfo.totalSafetyDay;
            if (totalSafetyDay == null || totalSafetyDay == 0) {
                flag = true;
                this.$message.error("总安全天数不能为空和0");
                return flag;
            }
            var amzSafetyDay = this.formInfo.amzSafetyDay;
            if (amzSafetyDay == null || amzSafetyDay == 0) {
                flag = true;
                this.$message.error("亚马逊安全库存天数不能为空和0");
                return flag;
            }

            var lastUnitsAvgDayRatio = this.formInfo.lastUnitsAvgDayRatio;
            var estUnitsAvgDayRatio = this.formInfo.estUnitsAvgDayRatio;
            // if (
            //   lastUnitsAvgDayRatio == null ||
            //   lastUnitsAvgDayRatio == 0 ||
            //   estUnitsAvgDayRatio == null ||
            //   estUnitsAvgDayRatio == 0
            // ) {
            //   flag = true;
            //   this.$message.error("加权历史日均占比 和加权预测日均占比 不能为空");
            //   return flag;
            // }
            if (lastUnitsAvgDayRatio + estUnitsAvgDayRatio != 1) {
                flag = true;
                this.$message.error("加权历史日均占比 + 加权预测日均占比 != 1");
                return flag;
            }

            var bigLastUnitsAvgDay = this.formInfo.bigLastUnitsAvgDay;
            var bigItemKeyId = this.formInfo.bigItemKeyId;
            var bigEstUnitisAvgDayRatio = this.formInfo.bigEstUnitisAvgDayRatio;

            if (
                bigLastUnitsAvgDay == null ||
                bigLastUnitsAvgDay == 0 ||
                bigItemKeyId == null ||
                bigItemKeyId == 0 ||
                bigEstUnitisAvgDayRatio == null ||
                bigEstUnitisAvgDayRatio == 0.0
            ) {
                flag = true;
                this.$message.error(
                    "提醒设置一  近** 天日均 >= 天预测日均 百分之** 不能为空 "
                );
                return flag;
            }

            var samllLastUnitsAvgDay = this.formInfo.samllLastUnitsAvgDay;
            var samllItemKeyId = this.formInfo.samllItemKeyId;
            var samllEstUnitsAvgDayRatio = this.formInfo
                .samllEstUnitsAvgDayRatio;

            if (
                samllLastUnitsAvgDay == null ||
                samllLastUnitsAvgDay == 0 ||
                samllItemKeyId == null ||
                samllItemKeyId == 0 ||
                samllEstUnitsAvgDayRatio == null ||
                samllEstUnitsAvgDayRatio == 0.0
            ) {
                flag = true;
                this.$message.error(
                    "提醒设置二 近**天日均 <= 天预测日均百分之**不能为空 "
                );
                return flag;
            }

            return flag;
        },
        saveOneRowProductInventory() {
            var url =
                this.GLOBAL.inventory +
                "/inventoryByModelNumberHandler/saveProductInventoryInfo";
            this.$ajax({
                method: "post",
                url: url,
                data: this.formInfo
            }).then(res => {
                if (res.data.code == 200) {
                    console.log(res.data.data);
                    this.$message.success(res.data.msg);
                } else if (res.data.code == 500) {
                    this.$message.error(res.data.msg);
                }
            });
            this.clearFormInfo();
        },
        //获取数据列
        findColumn() {
            this.$ajax
                .get(
                    this.GLOBAL.inventory +
                        "/viewTableHandler/findViewTableColumnName?bisonViewTableId=6&userId=" +
                        this.$store.getters.userId
                )
                .then(res => {
                    if (res.data.code === ERR_OK) {
                        this.column(
                            JSON.parse(res.data.data.viewTableColumnJson)
                        );
                    }
                });
        },
        column(val) {
            this.dataColumnShow = {};
            this.dataColumn = val;
            this.allDataColumn.forEach(ele => {
                if (val.indexOf(ele.property) === -1) {
                    this.dataColumnShow[ele.property] = true;
                }
            });
        },
        // 更新数据列
        upDataColumn(val) {
            this.$ajax({
                method: "post",
                url:
                    this.GLOBAL.inventory +
                    "/viewTableHandler/saveViewTableColumnName",
                data: {
                    bisonViewTableId: "6",
                    userId: this.$store.getters.userId,
                    viewTableColumnList: val
                }
            }).then(res => {
                if (res.data.code === ERR_OK) {
                    this.column(val);
                    this.search();
                    this.$refs.dataColum.dataColumhide();
                } else {
                    this.$message.error(res.data.msg);
                }
            });
        },
        // 下载
        getDataxlsx() {
            var data = {
                businessUnitIdList: this.searchInfo.businessUnitId,
                productCategoryIdList: this.searchInfo.productCategoryId,
                productIdList: this.searchInfo.productId,
                countryIdList: this.searchInfo.countryId,
                brandIdList: this.searchInfo.brandId,
                afnFulfillableQuantityNull: this.afnFulfillableQuantityNull
            };
            this.$ajax({
                method: "post",
                url:
                    this.GLOBAL.inventory +
                    "/inventoryBySellerHandler/findSellerInventoryList",
                data: data
            }).then(res => {
                if (res.data.code == 200) {
                    let data = [];
                    res.data.data.data.forEach(item => {
                        let items = item;
                        items.shippedAndReceivingQuantity =
                            item.amzInventoryVO.shippedAndReceivingQuantity;
                        items.amzTotalInventoryQuantity =
                            item.amzInventoryVO.amzTotalInventoryQuantity;
                        items.amzFulfillableQuantityDay =
                            item.amzInventoryVO.amzFulfillableQuantityDay;
                        items.amzReplenishmentDay =
                            item.amzInventoryVO.amzReplenishmentDay;
                        items.amzInventoryVOreplenishmentQuantity =
                            item.amzInventoryVO.replenishmentQuantity;
                        items.fnInventoryQuantity =
                            item.fnInventoryQuantity;
                        items.upcInventoryQuantity =
                            item.upcInventoryQuantity;
                        items.localInventoryTotalQuantity =
                            item.upcInventoryQuantity+ item.fnInventoryQuantity;
                        items.supplierName =
                            item.supplierInventoryVO.supplierName;
                        items.iTaxUnitPrice =
                            item.supplierInventoryVO.iTaxUnitPrice;
                        items.unclearedQuantity =
                            item.supplierInventoryVO.unclearedQuantity;
                        items.supplierInventoryTotalQuantity =
                            item.supplierInventoryVO.supplierInventoryTotalQuantity;
                        data.push(items);
                    });
                    this.$refs.download.getDataXlsx(data);
                } else {
                    this.$message.error(res.data.msg);
                }
            });
        }
    }
};
</script>


<style lang="stylus">
.SellerView-warp {
    .el-table .red_warning-row {
        background: #d6363c;
        color: white;

        &:hover {
            background: #b11117;

            >td {
                background: none;
            }
        }
    }

    .el-table .blue_warning-row {
        background: #9bf995;

        &:hover {
            background: #55d24d;

            >td {
                background: none;
            }
        }
    }

    .el-table .hover-row {
        color: black;
    }

    .el-table th {
        padding: 8 0;
    }

    .el-table th>.cell {
        line-height: 16px;
    }

    .el-table thead.is-group th {
        text-align: center;
    }

    .none {
        display: none;
    }
}
</style>
<style lang="stylus" scoped>
 .el-table{
     margin 10px 0
}

.el-form-item {
    display: inline-block;
    margin-bottom: 0;
}
</style>
